const { add, find, update, del, addAll, delAll } = require("../../server")
const chat = require('../../model/chat')


//查询全部数据 /admin/chat/chatAll
const getCtr = async (req, res, next) => {
    try {
        let data = await find(chat)
        res.send({
            code: 200,
            data,
        })
    } catch (err) {
        console.log(err);
        res.send({
            code: 400,
            message: '数据查询失败',
        })
    }
}



//增加 /admin/chat/chats
const postCtr = async (req, res, next) => {
    let data = req.body
    try {
        await add(chat, data)
        res.send({
            code: 200,
        })
    } catch (error) {
        console.log(error);
        res.send({
            code: 400,
            message: '信息发送失败'
        })
    }
}



// 删除数据
const delCtr = async (req, res, next) => {
    try {
        let bob = await del(chat, {
            id: req.body.id
        })
        if (bob == 0) {
            res.send({
                code: 400,
                message: '删除失败，请联系程序员'
            })
            return;
        }
        res.send({
            code: 200,
            message: '删除成功'
        })
    } catch (error) {
        console.log(error);
        res.send({
            code: 400,
            message: '删除失败，请联系程序员'
        })
    }
}

// 删除全部数据
const delCtr1 = async (req, res, next) => {
    try {
        let bob = await delAll(chat)
        res.send({
            code: 200,
            message: '删除成功'
        })
    } catch (error) {
        console.log(error);
        res.send({
            code: 400,
            message: '删除失败，请联系程序员'
        })
    }
}


module.exports = {
    getCtr, postCtr, delCtr, delCtr1
}